<%@ page language="java" pageEncoding="UTF-8" import="com.chinapost.util.AuthUtil"%>
<%@ taglib prefix="my" uri="/WEB-INF/tlds/permission.tld" %>
<%
	AuthUtil authUtil = new AuthUtil(session);
%>
<script type="text/javascript">
var yxsj_datagrid;
$(function ()
{
    yxsj_datagrid = $('#yxsj_manager_datagrid').datagrid(
        {
            url : '${pageContext.request.contextPath}/yxsjAction!datagrid.action',
            pagination : true,
            pageSize : 20,
            pageList :  [  10, 20, 30, 40, 50 ],
            border : false,
            fit: true,
            fitColumns: true,
            idField : 'cid',
            sortName : 'cbh',
            sortOrder : 'asc',
            checkOnSelect : false,
            selectOnCheck : true,
            columns : [[
                    {
                        title : 'id',
                        field : 'cid',
                        width : 100,
                        align : "center",
                        checkbox:true
                    },
                    {
                        title : '部门名称',
                        field : 'deptname',
                        align : "center",
                        width : 120
                    },
                    {
                        title : '用户编号',
                        field : 'cbh',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '用户名',
                        field : 'username',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '月份',
                        field : 'csbyf',
                        align : "center",
                        width : 60
                    },
                    {
                        title : '业务类别',
                        field : 'kindname',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '账号',
                        field : 'czh',
                        align : "center",
                        width : 100
                    },
                    {
                        title : '卡号(一本通)',
                        field : 'ckh',
                        align : "center",
                        width : 100
                    },
                    {
                        title : '开户局',
                        field : 'ckhj',
                        align : "center",
                        width : 70
                    },
                    {
                        title : '开户名',
                        field : 'cxm',
                        align : "center",
                        width : 70
                    },
                    {
                        title : '账户余额',
                        field : 'cye',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '存期',
                        field : 'ccq',
                        align : "center",
                        width : 50
                    },
                    {
                        title : '积分',
                        field : 'cjf',
                        align : "center",
                        width : 60
                    },
                    {
                        title : '营销奖励',
                        field : 'cyxfy',
                        align : "center",
                        width : 80
                    },
                    {
                        title : '开户日',
                        field : 'ckhr',
                        align : "center",
                        width : 70
                    },
                    {
                        title : '起息日',
                        field : 'cqxr',
                        align : "center",
                        width : 70
                    },
                    {
                        title : '状态',
                        field : 'czt',
                        align : "center",
                        width : 60
                    },
                    {
                        title : '备注',
                        field : 'cbz',
                        align : "center",
                        width : 60
                    },
                    {
                        field : 'action',
                        title : '操作',
                        width : 100,
                        align : 'center',
                        formatter : function (value, row, index)
                        {
                        	var str='';
                        	<%if (authUtil.isAuth("/yxsjAction!yxsjEdit.action")){%>
                        	str += sy.fs('<img onclick="yxsjEdit(\'{0}\');" src="{1}" style="cursor: pointer;" title="修改"/>&nbsp;',row.cid,'${pageContext.request.contextPath}/css/themes/icons/pencil.png');
                        	<%}%>
                        	<%if (authUtil.isAuth("/yxsjAction!delete.action")){%>
                        	str += sy.fs('<img onclick="yxsjDelete(\'{0}\');" src="{1}" style="cursor: pointer;" title="删除"/>&nbsp;',row.cid,'${pageContext.request.contextPath}/css/themes/icons/no.png');
                        	<%}%>
                        	return str;
                        }
                    }
                ]],
            toolbar : '#yxsj_toolbar'
        }
        );
    $('#yxsj_manager_dept').val('${sessionInfo.deptid}');
}
);

function yxsjEdit(cid)
{
    if (cid != undefined)
    {
        $('#yxsj_manager_datagrid').datagrid('unselectAll');
        $('#yxsj_manager_datagrid').datagrid('selectRecord', cid);
    }
    var rows = $('#yxsj_manager_datagrid').datagrid('getSelected');
    var p = parent.sy.dialog(
        {
            title : '修改营销数据',
            iconCls : 'icon-edit',
            href : '${pageContext.request.contextPath}/yxsjAction!yxsjEdit.action',
            width : 550,
            height : 330,
            buttons : [
                {
                    text : '保存',
                    iconCls : 'icon-save',
                    handler : function ()
                    {
                        var f = p.find('form');
                        f.form('submit',
                        {
                            url : '${pageContext.request.contextPath}/yxsjAction!edit.action',
                            success : function (d)
                            {
                                var json = $
                                    .parseJSON(d);
                                if (json.success)
                                {
                                    yxsj_datagrid.datagrid('reload');
                                    p.dialog('close');
                                }
                                parent.sy.messagerShow(
                                {
                                    msg : json.msg,
                                    title : '提示'
                                }
                                );
                            }
                        }
                        );
                    }
                }
            ],
            onLoad : function ()
            {
                var f = p.find('form');
                $('#yxsj_edit_userbh').combobox({
       			 url:'userAction!do_combobox.action?deptid='+rows.deptid,
       			 required:true,
       			 valueField:'cbh',
       			 textField:'cname'
       		 });
                f.form('load',rows);
            }
        }
        );
};
function yxsjAppend()
{
    var p = parent.sy.dialog(
        {
            title : '添加记录',
            iconCls : 'icon-append',
            href : '${pageContext.request.contextPath}/yxsjAction!yxsjAdd.action',
            width : 380,
            height : 300,
            buttons : [
                {
                    text : '保存',
                    iconCls : 'icon-save',
                    handler : function ()
                    {
                        var f = p.find('form');
                        f.form('submit',
                        {
                            url : '${pageContext.request.contextPath}/yxsjAction!add.action',
                            success : function (d)
                            {
                                var json = $.parseJSON(d);
                                if (json.msg.indexOf("成功")>0)
                                {
                                    yxsj_datagrid.datagrid('reload');
                                    f.find('input[name="czh"]').val('').focus();
                                }else{
                                	f.find('input[name="czh"]').select();
                                }
                                parent.sy.messagerShow(
                                {
                                    msg : json.msg,
                                    title : '提示'
                                }
                                );
                            }
                        }
                        );
                    }
                }
            ]
        }
        );
}
function yxsjRemove()
{
    var rows = yxsj_datagrid.datagrid('getChecked');
    var ids = [];
    if (rows.length > 0)
    {
        parent.sy.messagerConfirm('请确认', '是否删除当前所选记录？', function (r)
        {
            if (r)
            {
                for (var i = 0; i < rows.length; i++)
                {
                    ids.push(rows[i].cid);
                }
                $.ajax(
                {
                    url : '${pageContext.request.contextPath}/yxsjAction!delete.action',
                    data :
                    {
                        ids : ids.join(',')
                    },
                    dataType : 'json',
                    success : function (d)
                    {
                        yxsj_datagrid.datagrid('load');
                        $('#yxsj_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
                        parent.sy.messagerShow(
                        {
                            title : '提示',
                            msg : d.msg
                        }
                        );
                    }
                }
                );
            }
        }
        );
    }
    else
    {
        parent.sy.messagerAlert('提示', '请选择要删除的记录！', 'error');
    }
};

function yxsjDelete(cid)
{
    $('#yxsj_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
    $('#yxsj_manager_datagrid').datagrid('checkRow', $('#yxsj_manager_datagrid').datagrid('getRowIndex', cid));
    yxsjRemove();
}



function _searchYxsj() {
	yxsj_datagrid.datagrid('load', sy.serializeObject($('#yxsj_search_form')));
	yxsj_datagrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
};
function _cleanSearchYxsj() {
	$('#yxsj_search_form  input').val('');
	$('#yxsj_manager_dept').combotree('setValue','${sessionInfo.deptid}');
	yxsj_datagrid.datagrid('load', sy.serializeObject($('#yxsj_search_form')));
	 $('#yxsj_manager_datagrid').datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
};

function Uploader(chunk,callBack){
	var addWin = $('<div style="overflow: hidden;"/>');
	var upladoer = $('<iframe/>');
	upladoer.attr({'src':'${pageContext.request.contextPath}/jsp/yxsj/uploader.jsp?chunk='+chunk,width:'100%',height:'100%',frameborder:'0',scrolling:'no'});
	addWin.window({
		title:"上传营销数据文件（可上传多个文件）",
		height:390,
		width:550,
		minimizable:false,
		modal:true,
		collapsible:false,
		maximizable:false,
		resizable:false,
		content:upladoer,
		onClose:function(){
			var fw = GetFrameWindow(upladoer[0]);
			var files = fw.files;
			var sbyf = fw.csbyf;
			$(this).window('destroy');
			callBack.call(this,files,sbyf);
		},
		onOpen:function(){
			var target = $(this);
			setTimeout(function(){
				var fw = GetFrameWindow(upladoer[0]);
				fw.target = target;
			},100);
		}
	});
}

/**
 * 根据iframe对象获取iframe的window对象
 * @param frame
 * @returns {Boolean}
 */
function GetFrameWindow(frame){
	return frame && typeof(frame)=='object' && frame.tagName == 'IFRAME' && frame.contentWindow;
}
 
function makerUpload(chunk){
 Uploader(chunk,function(files,sbyf){
	 if(files && files.length>0){
			$.messager.progress({
				text : '上传'+files.length+'个文件完毕，正在导入数据...',
				interval : 100
			});
			$.post("yxsjAction!addYxsjs.action", {
				 filenames : files.join(','),
				 createuserid : '${sessionInfo.userId}',
				 deptid : '${sessionInfo.deptid}',
				 csbyf : sbyf
			}, function(rsp) {
				if (rsp.success) {
					yxsj_datagrid.datagrid('reload');
				}
				$.messager.progress('close');
				parent.sy.messagerAlert('提示', rsp.msg, 'info');
			}, "JSON");
	 }
 });
}

function export_yxsj() {
	var smonth=$('#yxsj_startMonth').val();
	var emonth=$('#yxsj_endMonth').val();
	if (smonth.length >0 && emonth.length >0){
	 url='${pageContext.request.contextPath}/yxsjAction!exportExcel.action';
	 $('#yxsj_search_form').attr('action',url);
     $('#yxsj_search_form').submit();
	}else{
		parent.sy.messagerAlert('提示', '请选择要导出数据的起始月份和结束月份！', 'error');
	}
}
</script>

<div class="easyui-layout" data-options="fit:true" style="overflow: hidden;">
	<div data-options="region:'north',title:'查询',iconCls:'icon-search'" style="height: 80px;padding:5px;overflow: hidden;">
		<div style="padding:5px">
			<form method="post" id="yxsj_search_form" style="display:inline">
			<span>所属单位：</span>
			<input id="yxsj_manager_dept" class="easyui-combotree" name="deptid" data-options="lines:true,url:'deptAction!do_combotree.action',panelHeight:280,required:true,editable:false,idField:'id',textField:'text',parentField:'pid'" style="width:150px"/>&nbsp;
			<span>人员编号：</span>
			<input type='text' id="yxsj_manager_userbh" name='cbh' style="width:80px" />&nbsp;&nbsp;
			<span>账号：</span>
			<input type='text' name='czh'  style="width:80px"/>&nbsp;
			<span>开始月份：</span>
			<input class="easyui-my97" type="text" id="yxsj_startMonth" name="startMonth"  data-options="dateFmt:'yyyyMM'" style="width:80px"/>&nbsp;
			<span>结束月份：</span>
			<input class="easyui-my97" type="text" id="yxsj_endMonth" name="endMonth"  data-options="dateFmt:'yyyyMM'" style="width:80px"/>&nbsp;
			</form>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="_searchYxsj();">查询</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onClick="_cleanSearchYxsj();;">清空</a>
			<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-excel'" onClick="export_yxsj();;">导出</a>
		</div>
	</div>
	<div data-options="region:'center',title:'营销数据列表',border:true,iconCls:'icon-menu'" style="overflow: hidden;">
		<table id="yxsj_manager_datagrid"></table>
	</div>
</div>
<div id="yxsj_toolbar" style="display: none;" >
<table  class="toolbar" >
	<tr>
		<my:per url="/yxsjAction!yxsjAdd.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-append',plain:true" onclick="yxsjAppend();">新增</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
		<my:per url="/yxsjAction!delete.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-delete',plain:true" onclick="yxsjRemove()">批量删除</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
		<my:per url="/yxsjAction!yxsjImport.action">
		<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-import',plain:true" onclick="makerUpload(false);">批量导入</a></td>
		<td><div class="datagrid-btn-separator"></div></td>
		</my:per>
	</tr>
</table>
</div>
